package org.rc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.rc.dto.RoleAddDto;
import org.rc.dto.RolePageDto;
import org.rc.dto.RoleUpdateDto;
import org.rc.entity.Role;
import org.rc.entity.RoleMenu;
import org.rc.mapper.RoleMapper;
import org.rc.service.RoleMenuService;
import org.rc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper,Role> implements RoleService {

    @Autowired
    RoleMenuService roleMenuService;

    @Override
    public void add(RoleAddDto dto) {
        Role role = Role.getInstance(dto);
        this.save(role);
        if(CollectionUtil.isNotEmpty(dto.getMenus())){
            roleMenuService.batchSave(dto.getMenus().stream().map(menuId-> RoleMenu.getInstance(role.getId(),menuId)).collect(Collectors.toList()));
        }
    }

    @Override
    public void updateRole(RoleUpdateDto roleUpdateDto) {
        Role role = Role.getInstance(roleUpdateDto);
        this.updateById(role);
        if(CollectionUtil.isNotEmpty(roleUpdateDto.getMenus())){
            roleMenuService.deleteByRoleId(role.getId());
            roleMenuService.batchSave(roleUpdateDto.getMenus().stream().map(menuId-> RoleMenu.getInstance(role.getId(),menuId)).collect(Collectors.toList()));
        }
    }

    @Override
    public void deleteRole(Long id) {
        this.removeById(id);
    }

    @Override
    public void deleteRoles(List<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public Page<Role> getRolePage(RolePageDto rolePageDto) {
        Page<Role> page = new Page<>(rolePageDto.getPageNo(),rolePageDto.getPageSize());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(rolePageDto.getName()),"name",rolePageDto.getName())
                .notIn("id",1,2);
        return this.baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<Role> getRolesNotInAdminAndDcz() {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.notIn("id",1,2);
        return list(queryWrapper);
    }

    @Override
    public Role getByRoleId(Long id) {
        return getById(id);
    }

    @Override
    public List<Role> getByRoleName(String name,Long id) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name.trim()).ne("id",id);
        return list(queryWrapper);
    }

    @Override
    public List<Role> getByRoleName(String name) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name.trim());
        return list(queryWrapper);
    }

    @Override
    public List<Role> getByRoleIds(List<Long> ids) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        return list(queryWrapper);
    }
}
